package com.like.hospitalworld;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.like.fragment.RNDocFragment;
import com.like.fragment.RNStaffFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_real_name_auth)
public class RealNameAuthActivity extends BaseActivity {

    @ViewInject(R.id.role_dropdown)
    private ViewGroup mRoleDropdown;
    @ViewInject(R.id.role_selected_value)
    private TextView mRoleSelectedValue;

    private PopupWindow mRolePopup;
    private RNStaffFragment mStaffFragment;
    private RNDocFragment mDocFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Event(R.id.role_dropdown)
    private void roleClick(View v) {
        showRolePopup();
        v.setSelected(true);
    }

    private void showRolePopup() {
        if (mRolePopup == null) {
            View contentView = LayoutInflater.from(m.mContext).inflate(R.layout.popup_role, null, false);
            mRolePopup = new PopupWindow(contentView, mRoleDropdown.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, false);
            mRolePopup.setBackgroundDrawable(new BitmapDrawable());
            mRolePopup.setOutsideTouchable(true);
            mRolePopup.setFocusable(true);
            mRolePopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mRoleDropdown.setSelected(false);
                }
            });
            TextView staff = (TextView) contentView.findViewById(R.id.staff);
            TextView doc = (TextView) contentView.findViewById(R.id.doc);
            staff.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRoleSelectedValue.setText("强生员工");
                    showStaffFragment();
                    mRolePopup.dismiss();
                }
            });
            doc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRoleSelectedValue.setText("医生");
                    showDocFragment();
                    mRolePopup.dismiss();
                }
            });
        }
        mRolePopup.showAsDropDown(mRoleDropdown, 0, -18);
    }

    private void showStaffFragment() {
        if (mStaffFragment == null)
            mStaffFragment = new RNStaffFragment();
        if (mStaffFragment.isAdded() && mStaffFragment.isVisible())
            return;
        if (mDocFragment != null && mDocFragment.isAdded() && mDocFragment.isVisible())
            getSupportFragmentManager().beginTransaction().replace(R.id.content_container, mStaffFragment).commit();
        else
            getSupportFragmentManager().beginTransaction().add(R.id.content_container, mStaffFragment).commit();
    }

    private void showDocFragment() {
        if (mDocFragment == null)
            mDocFragment = new RNDocFragment();
        if (mDocFragment.isAdded() && mDocFragment.isVisible())
            return;
        if (mStaffFragment != null && mStaffFragment.isAdded() && mStaffFragment.isVisible())
            getSupportFragmentManager().beginTransaction().replace(R.id.content_container, mDocFragment).commit();
        else
            getSupportFragmentManager().beginTransaction().add(R.id.content_container, mDocFragment).commit();
    }

}
